package com.xue.action.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.xue.AppContext;
import com.xue.common.vo.ParamMap;
import com.xue.common.vo.ResultMap;
import com.xue.pojo.sys.SysMenu;
import com.xue.pojo.sys.SysUser;
import com.xue.service.sys.ISysMenuService;

//2018-12-28 15:14:49
//@RequestMapping(value = SysUserMenuController.PATH)
//@RestController("SysUserMenuController")
public class SysUserMenuController {
	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SysUserMenuController.class);
	public static final String PATH = "sys/usermenu";

	@Autowired
	private ISysMenuService service;

	/* 分页查询 */
	@PostMapping("/listPage")
	public Map<String, Object> listPage(@RequestBody ParamMap pm) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("codeArray", AppContext.getLoginUser().getMenuIds());
		params.put("pid", "0");
		params.put("resourceType", 1);
		List<SysMenu> parentMenus = service.queryParent(params);
		params.remove("pid");
		params.put("nPid", "0");
		List<SysMenu> subMenus = service.querySub(params);
		List<SysMenu> menus = new ArrayList<SysMenu>(subMenus.size() + parentMenus.size());
		for (int i = 0; i < parentMenus.size(); i++) {
			menus.add(parentMenus.get(i));
			for (int j = 0; j < subMenus.size(); j++) {
				if (parentMenus.get(i).getId().equals(subMenus.get(j).getPid())) {
					menus.add(subMenus.get(j));
				}
			}
			subMenus.removeAll(menus);
		}
		SysUser loginUser = AppContext.getLoginUser();
		params.put("userid", loginUser.getUserid());
		List<String> shoucangIds = service.loadUserShouCangMenuIds(params);
		return ResultMap.get().addData("list", menus).addData("shoucang", shoucangIds);

	}

	/* 删除 */
	@PostMapping("/deleteById")
	public void deleteByIds(@RequestBody ParamMap paramMap) {
		String menuId = paramMap.getS("menuId");
		if (StringUtils.hasText(menuId)) {
			Map<String, Object> params = new HashMap<String, Object>();
			SysUser loginUser = AppContext.getLoginUser();
			params.put("userid", loginUser.getUserid());
			params.put("menuid", menuId);
			if (service.checkUserMenuId(params) > 1) {
				service.deleteUserShouCang(params);
			}
		}
	}

	/* 添加收藏 */
	@PostMapping("/saveUserMenu")
	public void saveUserMenu(@RequestBody ParamMap paramMap) {
		String menuId = paramMap.getS("menuId");
		if (StringUtils.hasText(menuId)) {
			SysUser loginUser = AppContext.getLoginUser();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userid", loginUser.getUserid());
			params.put("menuid", menuId);
			service.saveUserShouCang(params);
		}

	}
}